package fr.mbds.first.wallet.Activity;

import java.util.Map;

import org.mbds.wolf.seql.ISeqlCallBack;
import org.mbds.wolf.seql.ISeqlController;
import org.mbds.wolf.seql.SeqlController;
import org.mbds.wolf.seql.exceptions.APDUError;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import fr.mbds.first.wallet.R;

public class PINActivity extends Activity implements ISeqlCallBack {

	
	protected ISeqlController ctrl = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pin);
		
		DebitCreditFirstAppli act = (DebitCreditFirstAppli)getApplication();
		ctrl = act.getController();
		ctrl.setCallback(this);
		
		final EditText codePINEditText = (EditText) findViewById(R.id.EditText_PIN_PINCode);
		Button validerButton = (Button) findViewById(R.id.Button_PIN_Ok);
		
		validerButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				String mdp = codePINEditText.getText().toString();
				if(mdp.equals("")){
					String errorMessage = getResources().getString(R.string.error_nopincode);
					Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_LONG).show();
				}else{
					if(isPinValid(mdp)){
						Intent intent = new Intent(PINActivity.this, IdentificationActivity.class);
						startActivity(intent);
						finish();
					}else{
						String errorMessage = getResources().getString(R.string.error_unvalidpincode);
						Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_LONG).show();
					}
				}

			}
        	
        });
	}
	
	private boolean isPinValid(String mdp){
		return true;
	}
	
	@Override
	public void onResume(){
		super.onResume();
		ctrl.setCallback(this);
		if(!ctrl.isServiceConnected())
			ctrl.initService();
	}

	@Override
	public void onAPDUError(APDUError arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onBadRequest(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConfigError() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConnected() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onIOException(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNoApplet() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNoReader() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNoSecureElement() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNotConnected() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPINRequired() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResponse(Map<String, Object> arg0, int arg1) {
		// TODO Auto-generated method stub
	}
	
}
